import os.path, cv2# https://pypi.org/project/opencv-python/

def 黑白化(原圖路徑):
	原圖 = cv2.imread(原圖路徑)
	灰圖 = cv2.cvtColor(原圖, cv2.COLOR_BGR2GRAY)
	# _,黑白圖 = cv2.threshold(灰圖, 113, 255, cv2.THRESH_BINARY)
	糊圖 = cv2.medianBlur(灰圖, 5)
	# 黑白圖 = cv2.adaptiveThreshold(灰圖, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
	黑白圖 = cv2.adaptiveThreshold(糊圖, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
	路徑, 後綴 = os.path.splitext(原圖路徑)
	cv2.imwrite(路徑 +'-黑白.png', 黑白圖)
黑白化('圖片_20231010151946.jpg')